import { Component, OnInit } from '@angular/core';
import{ SQLService }from '../sql.service'
import {Utils} from '.././utils'
@Component({
  selector: 'app-sqlite',
  templateUrl: './sqlite.page.html',
  styleUrls: ['./sqlite.page.scss'],
})
export class SqlitePage implements OnInit {
  info :string = "准备中...";
  expenses ;totalIncome;balance;totalExpense;
  data= {
    date:'11',type:'11',description:'11',amount:11
  }
  constructor(    private sqlService:SQLService,
  private utils:  Utils,
    ) { }
  ngOnInit() {
    this.createTable() ;
  }
  getData(){
    this.selectAllUsers();
  }
  saveData() {
    this.insertUser() ;
  }

  createTable()
  {
    let sql = 'create table IF NOT EXISTS user(id VARCHAR(32), name VARCHAR(64), sex NUMBER(8))';
    this.sqlService.executeSql(sql).then((data) => {
      console.log(data);
      // alert("创建表成功");
    }).catch((err) => {
      console.log(err);
      // alert("创建表失败");

    });
  }

  insertUser()
  {
    let id = this.utils.getSequence();
    let sql = "insert into user values('"+ id +"', 'JoyoDuan', 23)";
    this.sqlService.executeSql(sql).then((data) => {
      console.log(data);
      this.info  = this.info + '---新增' ;

      // alert("insert成功");
    }).catch((err) => {
      console.log(err)
      this.info  = this.info + '---新增失败' ;

      // alert("insertshibai");
    });
  }
  selectAllUsers()
  {
    this.info  = this.info + '---查询' ;
    const sql = "select * from  user  ";
    this.sqlService.executeSql(sql).then((data) => {
      console.log(data);
      const datarows =data.res.rows;
      // this.info  = this.info +"--------"+JSON.stringify(data)+"===============";
      for (let i = 0; i <  datarows.length;i++) {
        var row =  datarows.item(i);
      // for (const row of datarows) {
        // this.info  = this.info +"--------"+JSON.stringify(row);
        const id = row.id;
        const imgurl = row.name;
        const filename = row.sex;
        // this.upload(id, imgurl, filename);
        // this.info  = this.info    + '---' + id + '----' + imgurl + '---' + filename;
      }
      // alert("查询成功"+data.res.rows.length);
    }).catch((err) => {
      this.info  = this.info + '---' +err.err.message;
      console.log(err)
      alert("查询失败"+err.err.message);
    });
  }
}
